﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using COMOSOFT.OrderBusiness.Forms;
using System.Data.SqlClient;
using DevExpress.XtraBars.Helpers;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using System.IO;
using COMOSOFT.Class;
using COMOSOFT.OrderBusiness;
using COMOSOFT.MatarialManagement.Forms;
using COMOSOFT.JobManagement.Froms;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraNavBar;
using DevExpress.XtraBars.Alerter;

namespace COMOSOFT.Main
{
    public partial class FrmMain : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        ClassShare cls = new ClassShare("FrmMain");
        TabGeneralView frGV = null;
        FrmFactoryPurchaseMaster frFPM = null;
        FrmStyleDetails frSD = null;
        FrmMAGeneralView frMAGV = null;
        FrmMALibrary frMALib = null;
        FrmJobManagement frJobM = null;
        FrmJobGeneral frJobGr = null;
        DataTable dtbJob;       
        string _pages = null;
        object month, cid,sup;
        int JID, timejob;
        List<string> Pages = new List<string>();
        public FrmMain()
        {            
            InitializeComponent();            
            DevExpress.UserSkins.BonusSkins.Register();
            #region [Skin]
            SkinHelper.InitSkinGallery(ribbonGallery_Job);
            SkinHelper.InitSkinGallery(ribbonGallery_Orderskin);
            SkinHelper.InitSkinGallery(ribbonGallery_MASkin);
            SkinHelper.InitSkinGallery(ribbonGallery_WHSkin);
            SkinHelper.InitSkinGallery(ribbonGallery_PMSkin);
            SkinHelper.InitSkinGallery(ribbonGallery_BMSkin);
            #endregion
        }
        #region [CheckTabPage on Ribbon]
        public void CheckTabPage()
        {
            try
            {
                DataTable dtb = cls.CheckEmployee(ClassShare.Usid);
                if (dtb.Rows.Count > 0)
                {
                    for (int n = 0; n < dtb.Rows.Count; n++)
                    {
                        string tabname = dtb.Rows[n][0].ToString();
                        int num = ribbon.Pages.Count;
                        for (int i = 0; i < num; i++)
                        {
                            if (Ribbon.Pages[i].Text == tabname)
                            {
                                Ribbon.Pages[i].Visible = true;
                            }
                        }                        
                    }
                }
                this.Text = Ribbon.SelectedPage.Text;
                CheckNavbarcontrol(Ribbon.SelectedPage.Text);
            }
            catch(Exception e)
            {
                XtraMessageBox.Show(e.Message);
            }
        }
        #endregion

        #region [Clear TabbedManager]
        private void ClearTabbed()
        {
            int numpage = xtraTabbedMdiManager_TabForm.Pages.Count;
            for (int i = 0; i < numpage; i++)
            {
                xtraTabbedMdiManager_TabForm.Pages[0].MdiChild.Close();
            }
        }
        #endregion

        #region[Ribbon CLick]
        private void ribbon_Click(object sender, EventArgs e)
        {
            try
            {
                string pagename = Ribbon.SelectedPage.Text;
                if (_pages != null && _pages != pagename)
                {
                    this.Text = pagename;
                    CheckNavbarcontrol(pagename);
                    _pages = pagename;
                    ClearTabbed();                   
                    navBarControl_menu.OptionsNavPane.NavPaneState = NavPaneState.Expanded;
                    switch (pagename)
                    {
                        case "Order Management":
                            ribbonPageGroup_FtyPurChase.Visible = false;
                            ribbonPageGroup_OrderDetails.Visible = true;
                            break;
                        case "Matarial Management":
                            ribbonPageGroup_Matarial.Visible = false;
                            ribbonPageGroup_MALibrary.Visible = true;
                            break;
                        case "WareHouse Management":
                            break;
                        case "Production Management":
                            break;
                        case "Business Management":
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        //Order Business
        
        #region [Refresh data]
        public void RefreshOrdermenu()
        {
            try
            {
                gridOrderMenu.DataSource = sp_OrderMenuTableAdapter.Fill(dataSetOrderManagement.sp_OrderMenu, ClassShare.Usid);
                grid_FtyPurMaster.DataSource = sp_FtyPurMasterLoadTableAdapter.Fill(dataSetOrderManagement.sp_FtyPurMasterLoad);
                frGV.RefreshData();                
            }
            catch
            {
                MessageBox.Show("Can't refresh! Please try again!");
            }
        }
        private void barButtonItem_stylerefresh_ItemClick(object sender, ItemClickEventArgs e)
        {
            RefreshOrdermenu();
        }
        #endregion

        #region [Top data]
        public void TopData()
        {
            gridOrderMenu.DataSource = sp_OrderMenuTableAdapter.Fill(dataSetOrderManagement.sp_OrderMenu, ClassShare.Usid);
        }
        #endregion

        #region [Check Navbarcontrol]
        private void CheckNavbarcontrol(string tabname)
        {
            for (int i = 0; i < navBarControl_menu.Groups.Count; i++)
            {
                navBarControl_menu.Groups[i].Visible = false;
            }
            switch (tabname)
            {
                case "Job Management":
                    navBarControl_menu.Groups["navBarGroup_JobList"].Visible = true;
                    break;
                case "Order Management":
                    navBarControl_menu.Groups["navBarGroup_General"].Visible = true;
                    navBarControl_menu.Groups["navBarGroup_Purchasing"].Visible = true;
                    break;
                case "Matarial Management":
                    navBarControl_menu.Groups["navBarGroup_Source"].Visible = true;
                    navBarControl_menu.Groups["navBarGroup_MAList"].Visible = true;
                    navBarControl_menu.Groups["navBarGroup_Booking"].Visible = true;
                    navBarControl_menu.Groups["navBarGroup_Blaning"].Visible = true;
                    navBarControl_menu.Groups["navBarGroup_Exception"].Visible = true;
                    break;
                case "WareHouse Management":
                    navBarControl_menu.Groups["navBarGroup_ImportMA"].Visible = true;
                    navBarControl_menu.Groups["navBarGroup_ImportProduct"].Visible = true;
                    navBarControl_menu.Groups["navBarGroup_ExportMA"].Visible = true;
                    navBarControl_menu.Groups["navBarGroup_ExportProduct"].Visible = true;
                    navBarControl_menu.Groups["navBarGroup_StockAndException"].Visible = true;
                    break;
                case "Production Management":
                    navBarControl_menu.Groups["navBarGroup_FollowUp"].Visible = true;
                    navBarControl_menu.Groups["navBarGroup_DeleveryPlaning"].Visible = true;
                    break;
                case "Business Management":
                    navBarControl_menu.Groups["navBarGroup_FollowUp"].Visible = true;
                    navBarControl_menu.Groups["navBarGroup_DeleveryPlaning"].Visible = true;
                    break;
            }
        }
        #endregion        

        #region [Order Menu]
        private void gridViewmenu_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                GridView view = sender as GridView;

                if (view.IsGroupRow(view.FocusedRowHandle) == true)
                {
                    if (view.GetRowLevel(view.FocusedRowHandle) == 0)
                    {
                        month = null;                    
                        cid = null;
                    }
                    else if (view.GetRowLevel(view.FocusedRowHandle) == 1)
                    {
                        month = Convert.ToInt32(view.GetFocusedDataRow()["Month"]);
                        cid = null;
                    }
                }
                else
                {
                    month = Convert.ToInt32(view.GetFocusedDataRow()["Month"]);
                    cid = view.GetFocusedDataRow()["Customer"].ToString();
                }

                if (frGV != null && frGV.IsDisposed == false)
                {
                    frGV.Select();
                    frGV.LoadGV(Convert.ToInt32(view.GetFocusedDataRow()["Year"]), month,cid);
                }
                else
                {
                    frGV = new TabGeneralView(this);
                    frGV.MdiParent = this;
                    frGV.Show();
                    frGV.LoadGV(Convert.ToInt32(view.GetFocusedDataRow()["Year"]), month, cid);
                }
                navBarControl_menu.OptionsNavPane.NavPaneState = NavPaneState.Collapsed;            
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region[Event Ribbon FtyPurChase]
        private void FtyPurChase_ItemClick(object sender, ItemClickEventArgs e)
        {
            switch (e.Item.Caption)
            {
                case "New":
                    frFPM = new FrmFactoryPurchaseMaster(-1) { MdiParent = this, Text = "PO: Add new Order" };                   
                    frFPM.LoadFPM();                    
                    frFPM.Show();
                    frFPM.NewFPM();               
                    break;
                case "Save":
                    try
                    {                        
                        frFPM = ActiveMdiChild as FrmFactoryPurchaseMaster;
                        frFPM.UpdateFPM();
                        frFPM.LoadFPM();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message + "\n Nothing to update !!!");
                    }                    
                    break;
                case "Delete":
                    if (frFPM != null && !frFPM.IsDisposed)
                    {
                        frFPM.DeleteFtyPurMaster();
                        this.sp_FtyPurMasterLoadTableAdapter.Fill(this.dataSetOrderManagement.sp_FtyPurMasterLoad);
                    }                           
                    break;
                case "Refresh":
                    this.sp_FtyPurMasterLoadTableAdapter.Fill(this.dataSetOrderManagement.sp_FtyPurMasterLoad);
                    break;
            }
        }
        #endregion

        #region[Purchasing Menu]
        private void gridView_Purchasing_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                int fpid = Convert.ToInt32(gridView_Purchasing.GetFocusedDataRow()[0]);
                string title ="PO: "+ gridView_Purchasing.GetFocusedDataRow()["PONo"].ToString();
                ClassShare.fpid = fpid;               
                int flag=0;
                foreach(string item in Pages)
                {                   
                    if (frFPM != null && frFPM.IsDisposed == false && title == item)
                    {
                        frFPM.Select();
                        flag=1;
                        break;
                    }
                }
                if (flag == 0)
                {                    
                    frFPM = new FrmFactoryPurchaseMaster(fpid);
                    frFPM.MdiParent = this;
                    frFPM.Text = title;
                    frFPM.LoadFPM();
                    frFPM.Show();
                    Pages.Add(title);                  
                }                    
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }        
        }
        #endregion

        //-----------------

        #region [Form Load]
        private void FrmMain_Load(object sender, EventArgs e)
        {
            this.employeeTableAdapter.Fill(dataSet_JobManagement.Employee,ClassShare.Usid);
            this.sp_JobMenuTableAdapter.Fill(this.dataSet_JobManagement.sp_JobMenu, ClassShare.Usid);
            this.sp_MALibraryMenuTableAdapter.Fill(this.dataSetMatarial1.sp_MALibraryMenu);
            this.sp_MAListMasterLoadTableAdapter.Fill(this.dataSetMatarial.sp_MAListMasterLoad);
            this.sp_FtyPurMasterLoadTableAdapter.Fill(this.dataSetOrderManagement.sp_FtyPurMasterLoad);
            ribbonPageGroup_FtyPurChase.Visible = false;
            _pages = ribbon.SelectedPage.Text;
            dtbJob = sp_JobMenuTableAdapter.GetData(ClassShare.Usid);

            // Job            
            frJobGr = new FrmJobGeneral(this) { MdiParent = this };
            frJobGr.Show();           
        }
        #endregion

        //Material Management

        #region [Material Menu]
        private void gridView_MAList_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                int malid = Convert.ToInt16(gridView_MAList.GetFocusedDataRow()["MALID"]);                
                frSD = new FrmStyleDetails(malid);
                frSD.MdiParent = this;
                frSD.LoadData();
                frSD.Text = "MAPO: " + gridView_MAList.GetFocusedDataRow()["MALNo"].ToString();
                navBarControl_menu.OptionsNavPane.NavPaneState = NavPaneState.Collapsed;
                frSD.Show();
                frSD.RunBackroundWorker();   
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region [Refresh Material]
        private void RefreshMaterial()
        {
            sp_MAListMasterLoadTableAdapter.Fill(dataSetMatarial.sp_MAListMasterLoad);
        }
        #endregion

        #region [Event Ribbon Material]
        private void Matarial_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                switch (e.Item.Caption)
                {
                    case "New Material":
                        frSD = new FrmStyleDetails(-1) { MdiParent = this, Text = "Add new Matarial" };                      
                        frSD.Show();
                        frSD.AddNew();
                        frSD.LoadData();
                        navBarControl_menu.OptionsNavPane.NavPaneState = NavPaneState.Collapsed;
                        break;
                    case "Save":
                        try
                        {
                        frSD = ActiveMdiChild as FrmStyleDetails;
                        frSD.UpdateData();
                        frSD.LoadData();                            
                        }
                        catch (Exception ex)
                        {
                             MessageBox.Show(ex.Message + "\n Nothing to update !!!");
                        }
                        break;
                      
                    case "Delete":
                        frSD = ActiveMdiChild as FrmStyleDetails;
                        frSD.DeleteMaster();
                        RefreshMaterial();
                        break;
                    case "Refresh":
                        RefreshMaterial();
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region [Change Item Menu]     

        //*********************************************************************************
        private void navBarControl_menu_Change(object sender, EventArgs e)
        {
            switch (navBarControl_menu.ActiveGroup.Caption)
            {
                //OrderDetails
                case "Job List":
                    break;
                case "General":
                    ribbonPageGroup_OrderDetails.Visible = true;
                    ribbonPageGroup_FtyPurChase.Visible = false;
                    break;
                case "Purchasing View":
                    ribbonPageGroup_FtyPurChase.Visible = true; ;
                    ribbonPageGroup_OrderDetails.Visible = false;
                    break;
                //-----------------
                //Material
                case "Material Library":
                    ribbonPageGroup_MALibrary.Visible = true;
                    ribbonPageGroup_Matarial.Visible = false;
                    break;
                case "Material List":
                    ribbonPageGroup_MALibrary.Visible = false;
                    ribbonPageGroup_Matarial.Visible = true;
                    break;
                //-----------------
                default:
                    break;
            }
            navBarControl_menu.OptionsNavPane.NavPaneState = NavPaneState.Expanded;
            ClearTabbed();
        }      
        //*************************************************************************************
        
        #endregion

        #region[MALibrary]
        private void gridView_MALibrary_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                GridView view = sender as GridView;
                int year = Convert.ToInt32(view.GetFocusedDataRow()["Year"]);
                if (view.IsGroupRow(view.FocusedRowHandle) == true)
                {
                    if (view.GetRowLevel(view.FocusedRowHandle) == 0)
                    {
                        sup = null;
                    }                    
                }
                else
                    sup = Convert.ToInt32(view.GetFocusedDataRow()["MASUPID"]);

                if (frMAGV != null && frMAGV.IsDisposed == false)
                {
                    frMAGV.Select();
                    frMAGV.LoadMAGV(year, sup);
                }
                else
                {
                    frMAGV = new FrmMAGeneralView(this);
                    frMAGV.MdiParent = this;
                    frMAGV.Show();
                    frMAGV.LoadMAGV(year, sup);
                }
               navBarControl_menu.OptionsNavPane.NavPaneState = NavPaneState.Collapsed;
            }
            catch
            {
                MessageBox.Show("This Supplier Unused!!!","Value DBNull...",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
        }
        #endregion

        //Material Library
        #region [Update Material Libray]
        private void MALibrary_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                switch (e.Item.Caption)
                {
                    case "New":
                        frMALib = new FrmMALibrary(-1);
                        frMALib.MdiParent = this;
                        frMALib.Text = "Add new Material Library";                        
                        frMALib.Show();
                        frMALib.AddNew();
                        frMALib.LoadData();
                        navBarControl_menu.OptionsNavPane.NavPaneState = NavPaneState.Collapsed;
                        break;
                    case "Save":
                        try
                        {
                            frMALib = ActiveMdiChild as FrmMALibrary;
                            frMALib.UpdateMAL();
                            frMALib.LoadData();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message + "\n Nothing to update !!!");
                        }
                        break;
                    case "Delete":
                        frMALib = ActiveMdiChild as FrmMALibrary;
                        frMALib.DeleteMALib();
                        sp_MALibraryMenuTableAdapter.Fill(dataSetMatarial.sp_MALibraryMenu);                        
                        frMALib.LoadData();
                        break;
                    case "Refresh":
                        sp_MALibraryMenuTableAdapter.Fill(dataSetMatarial.sp_MALibraryMenu);                                           
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion       

        //-------------------
        //Job Management
        #region [Event Menu]
        private void gridView_JobManage_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                GridView view = sender as GridView;
                if (!view.IsGroupRow(view.FocusedRowHandle))
                {
                    JID = Convert.ToInt32(view.GetFocusedDataRow()["JID"]);
                }
                //if (frJobM != null && !frJobM.IsDisposed)
                //{
                //    frJobM.Select();
                //    frJobM.LoadData();
                //}
                //else
                //{
                   // cls.UpdateData("sp_JobStatusUpdate", new string[] { "@JID", "@Status" }, new object[] { JID, "Pending" });
                    sp_JobMenuTableAdapter.Fill(dataSet_JobManagement.sp_JobMenu, ClassShare.Usid);
                    dtbJob = sp_JobMenuTableAdapter.GetData(ClassShare.Usid);
                    frJobM = new FrmJobManagement(JID);
                    frJobM.MdiParent = this;
                    frJobM.Show();
                    frJobM.LoadData();
                //}
            }
            catch
            {
                MessageBox.Show("Disconnect Database! Try connect to again!", "Disconnect...", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

        #region [Event Ribbon Job]
        private void JobItem_Click(object sender, ItemClickEventArgs e)
        {
            switch (e.Item.Caption)
            {
                case "New Job":
                    frJobM = new FrmJobManagement(-1) { MdiParent = this, Text = "Add new Job" };                    
                    frJobM.Show();
                    frJobM.AddNew();
                    frJobM.LoadData();
                    frJobM.CheckButton();
                    break;
                case "Send":
                    frJobM = ActiveMdiChild as FrmJobManagement;
                    frJobM.UpdateJob();                  
                    //frJobM.LoadData();
                    break;
                case "Delete":
                    break;
                case "Refresh":
                    sp_JobMenuTableAdapter.Fill(dataSet_JobManagement.sp_JobMenu,ClassShare.Usid);
                    dtbJob = sp_JobMenuTableAdapter.GetData(ClassShare.Usid);
                    break;
                case "Job General":
                    if (frJobGr != null && !frJobGr.IsDisposed)
                        frJobGr.Select();
                    else
                    {
                        frJobGr = new FrmJobGeneral(this) { MdiParent=this};
                        frJobGr.Show();
                    }
                   // navBarControl_menu.OptionsNavPane.NavPaneState = NavPaneState.Collapsed;   
                    break;
            }
        }
        #endregion  

        #region [New Job]
        private void timer_job_Tick(object sender, EventArgs e)
        {
            timejob++;
            if (timejob == 30)
            {
                DataTable dtb = sp_JobMenuTableAdapter.GetData(ClassShare.Usid);
                if (dtb.Rows.Count > dtbJob.Rows.Count)
                {
                    int numjob = dtb.Rows.Count - dtbJob.Rows.Count;
                    AlertInfo info = new AlertInfo("Job Reminder " + numjob, "<<< You have a new job >>>");
                    alertControl_info.Show(this, info);
                    sp_JobMenuTableAdapter.Fill(dataSet_JobManagement.sp_JobMenu, ClassShare.Usid);
                    dtbJob = sp_JobMenuTableAdapter.GetData(ClassShare.Usid);
                }
                timejob = 0;
            }
        }
        #endregion
        //-------------------
  
        //System
        private void System_ItemClick(object sender, DevExpress.XtraBars.Ribbon.BackstageViewItemEventArgs e)
        {
            switch (e.Item.Caption)
            {
                case "Login":
                    FrmLogin frmlogin = new FrmLogin();
                    frmlogin.ShowDialog();
                    this.Hide();
                    break;
                case "Change Password":
                    FrmChangePassword frmchangepass = new FrmChangePassword();
                    frmchangepass.ShowDialog();                   
                    break;
            }
        }

        private void groupControl1_Paint(object sender, PaintEventArgs e)
        {

        }     

        //
    }
}
